import { uploadJoin, DeleteFile } from "../../utils/request/api";
import msg from "../../utils/msg";
import { getInterfaceRoute } from '../../utils/request/route';
import { formatTime, Throttle } from '../../utils/util';
import User from "../../utils/user";
const throttle = Throttle();
var user = new User;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    addContent: {
      title: '' as string,
      place: '' as string,
      start_time: '' as string,
      end_time: '' as string,
      people: '' as string,
      s_name: user.name,
      s_code: user.code,
      prize: '' as string,
    },
    showtime1: false,
    showtime2: false,
    minHour: 0,
    maxHour: 24,
    currentDate: new Date().getTime(),
    fileList: [] as any,
  },

  afterRead(event: any) {
    const file = event.detail.file;
    wx.uploadFile({
      url: getInterfaceRoute('upload'),
      filePath: file.url,
      name: 'prize',
      formData: { user: 'test' },
      success: (res) => {
        const { fileList = [] } = this.data;
        const url = JSON.parse(res.data).data;
        fileList.push({ ...file, url: url });
        this.setData({ fileList: fileList });
        if (this.data.addContent.prize == '') {
          this.data.addContent.prize = url.replace('http://127.0.0.1/csm/', '');
        }
        else {
          const newprize = this.data.addContent.prize + ',' + url.replace('http://127.0.0.1/csm/', '');
          this.data.addContent.prize = newprize;
          console.log(this.data.addContent.prize);
        }
      },
    });
  },

  //预览图片
  preview(event: any) {
    const index = event.detail.index;
    const imageUrl = this.data.fileList[index].url;
    if (this.data.fileList[index].type == 'file') {
      msg.show('此文件类型不可预览', '');
      return;
    }
    wx.previewImage({
      urls: [imageUrl]
    })
  },

  //删除已上传的图片
  delete(event: any) {
    const index = event.detail.index;
    const fileList = this.data.fileList;
    const file_path = fileList[index].url.replace('http://127.0.0.1/csm/', '');
    DeleteFile({ file_path: file_path });
    if (index >= 0 && index < fileList.length) {
      fileList.splice(index, 1);
      this.setData({
        fileList: fileList
      });
    }
  },

  //检查数据不能为空
  checkData() {
    let addContent: any = this.data.addContent;
    for (let key of Object.keys(addContent)) {
      if (!addContent[key]) {
        if (key == 'prize') {
          return true;
        }
        else {
          msg.show('有信息未填写', '请填写完整表单后再提交');
          return false;
        }
      }
    };
    return true;
  },
  //提交
  submit: async function () {
    if (!this.checkData()) return;
    msg.Loading('正在提交');
    await throttle(2000)
      .then(async () => {
        console.log('提交');
        await uploadJoin(this.data.addContent)
          .then(async () => {
            msg.hideLoading();
            await msg.show('提交成功', '');
            wx.navigateBack();
          })
          .catch(() => {
            msg.show('提交失败', '系统维护中');
          })
        msg.hideLoading();
      })
      .catch(() => {
        // 触发节流
        msg.show('请勿重复点击', '');
      })
  },

  //获取输入
  getInput(e: any) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      ['addContent.' + index]: e.detail.value,
    });
  },

  //获取开始时间
  getInputStart(event: any) {
    let addContent = this.data.addContent;
    addContent.start_time = formatTime(new Date(event.detail));
    console.log(addContent.start_time);
    this.setData({
      addContent: addContent,
      showtime1: false,
    });
  },

  //获取结束时间
  getInputEnd(event: any) {
    let addContent = this.data.addContent;
    addContent.end_time = formatTime(new Date(event.detail));
    this.setData({
      addContent: addContent,
      showtime2: false,
    });
  },

  //关闭弹窗
  onClose() {
    this.setData({
      showtime1: false,
      showtime2: false,
    })
  },

  //选择开始时间
  onChooseTime1() {
    this.setData({
      showtime1: true,
    })
  },

  //选择结束时间
  onChooseTime2() {
    this.setData({
      showtime2: true,
    })
  },

  //返回
  onClickLeft() {
    wx.navigateBack();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})